package name.huzhenbo.java.serialization;

import java.io.Serializable;
import java.util.Date;

class AnotherSerialPeriod implements Serializable {


    private final Date start;
    private final Date end;

    public AnotherSerialPeriod(Date start, Date end) {
        this.start = new Date(start.getTime());
        this.end = new Date(end.getTime());
        if (this.start.compareTo(end) > 0) {
            throw new IllegalArgumentException(this.start + " > " + this.end);
        }

    }

    public Date getStart() {
        return new Date(start.getTime());
    }

    public Date getEnd() {
        return new Date(end.getTime());
    }

    // This is more simple than implement readObject like in SerialObject.
    // Also, now we can make the start and end final.
    // But this creates an extra object.
    private Object readResolve() {
        return new AnotherSerialPeriod(start, end);
    }
}
